<?php
namespace app\blog\loglic;

use app\common\loglic\Model;

class Tag extends Model
{
    //初始查询参数
    private $args = [
        'where' => [
            ['tag_module','eq','blog'],
        ],
        'cache' => false,
        'order' => [
            'tag_id' => 'desc',
        ],
    ];

    //修改器（写入数据库前）
    private function dataSet($data=[])
    {
        $data['tag_slug']   = $this->autoSlug($data['tag_slug'],$data['tag_name']);
        $data['tag_module'] = 'blog';
        return ['tag'=>$data];
    }

    //获取器（查询数据库后）
    private function dataGet($data=[])
    {
        return $data;
    }

    /**
     * 添加
     * @version 1.0.0 首次引入
     * @param array $data 必需;数组格式;默认:空
     * @return int 返回自增ID或0
     */
    public function dataSave($data=[])
    {
        //数据初始化
        unset($data['tag_id']);
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'tag_name' => 'unique:tag,tag_name^tag_module',
            'tag_slug' => 'unique:tag,tag_slug^tag_module',
        ];
        $this->validateMsg = [
            'tag_name.unique' => '标签名称已存在',
            'tag_slug.unique' => '唯一标识已存在',
        ];
        //表单验证未通过
        if(!$this->validateData($data['tag'])){
            return false;
        }
        //写入数据库
        return $this->modelSave('common/Tag',$data);
    }

    //修改
    public function dataUpdate($data=[])
    {
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'tag_id' => 'number',
            'tag_name' => 'unique:tag,tag_name^tag_module',
            'tag_slug' => 'unique:tag,tag_slug^tag_module',
        ];
        $this->validateMsg = [
            'tag_id.number'   => 'ID错误',
            'tag_name.unique' => '标签名称已存在',
            'tag_slug.unique' => '唯一标识已存在',
        ];
        //表单验证未通过
        if(!$this->validateData($data['tag'])){
            return false;
        }
        //更新条件
        $where = [
            'tag_id' => $data['tag']['tag_id'],
        ];
        return $this->modelUpdate('common/Tag', $where, $data);
    }

    //查询数据列表
    public function all($args=[])
    {
        //合并参数
        $args = array_merge($this->args,$args);
        //查询数据
        return $this->modelSelect('common/Tag', $args);
    }

    //按条件分页查询多条（模型类）
    public function allPage($args=[], $pageSize=10, $pageNumber=1, $isTotal=false, $query=['page'=>1])
    {
        //合并参数
        $args = array_merge($this->args,$args);
        //初始参数
        array_push($args['where'],['tag_module','eq','blog']);
        //查询数据
        $this->pageData = $this->modelSelectPage('common/Tag', $args, $pageSize, $pageNumber, $isTotal, $query);
        if($this->pageData->isEmpty()){
            $this->error = '数据为空';
            return NULL;
        }
        //合并分页
        $result = $this->pageData->toArray();
        $result['pages'] = $this->pageData->render();
        return $result;
    }

    //按ID查询单个数据
    public function getId($id=0, $cache=false, $status=false)
    {
        $where = [
            ['tag_id','eq',$id],
        ];
        if($status){
            array_push($where,['tag_status','eq',$status]);
        }
        $result = $this->modelGet('common/Tag',[
            'cache' => $cache,
            'where' => $where,
        ]);
        return $this->dataGet($result);
    }

    //按SLUG查询单个数据
    public function getSlug($slug=0, $cache=false, $status=false)
    {
        $where = [
            ['tag_slug','eq',$slug],
            ['tag_module','eq','blog'],
        ];
        if($status){
            array_push($where,['tag_status','eq',$status]);
        }
        $result = $this->modelGet('common/Tag',[
            'cache' => $cache,
            'where' => $where,
            'with'  => $with,
        ]);
        return $this->dataGet($result);
    }

    /**
     * 通过标签名返回对应的ID与名称
     * @version 1.0.1 首次引入
     * @param string $tagName 必需;标签名;默认:空
     * @param bool $aotuSave 可选;查询无结果时是否自动新增;默认:false
     * @return array array或NULL
     */
    public function getOption($tagName='',$aotuSave=true)
    {
        $where = [
            ['tag_name','eq',$tagName],
            ['tag_module','eq','blog'],
        ];
        if($tagId = $this->dbValue('tag',$where,'tag_id')){
            return [$tagId=>$tagName];
        }
        if($aotuSave){
            $tagId = $this->dbSave('tag',[
                'tag_name'  => $tagName,
                'tag_slug'  => uniqid(),
                'tag_module'=> 'blog',
            ]);
            if($tagId){
                return [$tagId=>$tagName];
            }
        }
        return NULL;
    }

    //返回N条表列结果（键名为ID键值为Name）
    public function listOption($limt=10)
    {
        $limt = intval($limt);
        return db('tag')->limit($limt)->where([
            ['tag_status','eq','normal'],
            ['tag_module','eq','blog'],
        ])->order([
            'tag_count' => 'desc',
            'tag_id' => 'desc',
        ])->column('tag_name','tag_id');
    }

    //返回N条表列结果与当前TAGID的所有结果
    public function listIdsOption($ids=[],$limt=10)
    {
        if(!$ids){
            return $this->listOption($limt);
        }
        $resultIds = db('tag')->where([
            ['tag_status','eq','normal'],
            ['tag_module','eq','blog'],
            ['tag_id','in',$ids],
        ])->order([
            'tag_id' => 'desc',
        ])->column('tag_name','tag_id');
        //默认20个
        $result = $this->listOption($limt);
        //合并
        return $resultIds+$result;
    }
}